// ==UserScript==
// @name           phpBB3 Add Bookmark Icon Links to Topics
// @namespace      http://glenncarr.com/greasemonkey/phpbb
// @description    Adds bookmark icon link to each topic
// @include        *fantasybaseballcafe.com/forums*
// ==/UserScript==

(function() {

var topicLinks = document.evaluate("//a[contains(@href, 'viewtopic.php')]", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
if ( topicLinks.snapshotLength == 0 )
    return;

var cache = {};

GM_addStyle('\
a.gncForumLink { color: #b22222 }\
');

for ( var i = 0; i < topicLinks.snapshotLength; i++ )
{
    var a = topicLinks.snapshotItem( i );
    var base = a.href.match( /^(.*viewtopic\.php\?f=\d+)/ );
    if ( base && cache.hasOwnProperty( base[ 1 ] ) )
        continue;
        
    cache[ base[ 1 ] ] = 1;
    
    var bm = document.createElement( 'a' );
    bm.setAttribute( "class", "icon-bookmark" );
    bm.setAttribute( "title", "Bookmark topic" );
    bm.setAttribute( "style", "margin-left: 2px" );
    bm.href = a.href.replace( /^(.*viewtopic\.php\?f=\d+)/, '$1&bookmark=1' );
    bm.innerHTML = '&nbsp;';
    a.parentNode.insertBefore( bm, a.nextSibling );
}
})();